<template>
  <PageWrapper>
    <n-card :bordered="false" class="mb-3 proCard">
      <n-space align="center">
        <n-input-group style="width: 980px">
          <div style="margin-left: 10px !important;display: flex;align-items: center;">
            <div style="width: 160px;">电池编号：</div>
            <n-input
              style="width: 100%;margin-left: 20px;"
              v-model:value="params.number"
              clearable
              placeholder="请输入电池编号"
              @keyup.enter="reloadTable"
            />
          </div>

          <n-button type="primary" style="margin-left: 40px !important;" @click="reloadTable">
            <template #icon>
              <n-icon>
                <SearchOutlined />
              </n-icon>
            </template>
            查询
          </n-button>
        </n-input-group>
      </n-space>
    </n-card>
    <n-card :bordered="false" class="proCard">
      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        :actionColumn="actionColumn"
        :row-key="(row) => row.id"
        ref="basicTableRef"
        virtual-scroll
      >
      </BasicTable>
    </n-card>

    <n-modal v-model:show="showModal">
      <n-card
        style="width: 600px"
        title="日志记录"
        :bordered="false"
        size="huge"
        role="dialog"
        aria-modal="true"
      >
        <n-table :bordered="false" :single-line="false">
          <thead>
          <tr>
            <th>电池SN</th>
            <th>属性</th>
            <th>会员号</th>
            <th>备注</th>
            <th>时间</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item,index) in batteryAll" :key="index">
            <td>{{item.number}}</td>
            <td>{{item.status}}</td>
            <td>{{item.member_card}}</td>
            <td>{{item.remark}}</td>
            <td>{{item.created_at}}</td>
          </tr>
        </tbody>
        </n-table>
        <n-pagination
            v-model:page="batteryParams.page"
            v-model:page-size="batteryParams.pageSize"
            :page-count="batteryParams.totalPage"
            show-size-picker
            :page-sizes="[10, 20, 30, 40]"
            :on-update-page="batteryChangePage"
            :on-update-page-size="batteryChangePagesize"
          />
      </n-card>
    </n-modal>

    <CreateDrawer
      ref="createDrawerRef"
      :title="drawerTitle"
      :roleList="roleData"
      @change="reloadTable"
    />

  </PageWrapper>
</template>

<script lang="ts" setup>
  import { h, reactive, ref, onMounted } from 'vue';
  import { BasicColumn, BasicTable, TableAction } from '@/components/Table';
  import { batterylist,batteryloglist } from '@/api/device/battery';
  import { SearchOutlined } from '@vicons/antd';
  import { columns } from './columns';

  const basicTableRef = ref();
  const tableData = ref();
  const createDrawerRef = ref();
  const drawerTitle = ref('日志');
  const roleData = ref([]);

  const params = reactive({
    number: '',
  });

  const showModal = ref(false);
  const batteryParams = ref({
    page:1,
    pageSize:10,
    totalPage:1,
    number:'',
  });
  const batteryAll = ref([{
    number: '',
    status: '',
    remark: '',
    member_card: '',
    created_at: '',
  }]);

  const actionColumn: BasicColumn = reactive({
    width: 150,
    title: '操作',
    key: 'action',
    fixed: 'right',
    align: 'center',
    render(record) {
      return h(TableAction as any, {
        actions: [
          {
            label: '日志',
            onClick: handleEdit.bind(null, record),
          },
        ],
      });
    },
  });

  const loadDataTable = async (res) => {
    const result = await batterylist({ ...params, ...res });
    tableData.value = result.list;
    return result;
  };


  function reloadTable() {
    basicTableRef.value.reload();
  }

  function handleEdit(record: Recordable) {
    let number = record.number;
    batteryParams.value.number = number;

    batteryloglist(batteryParams.value).then((res)=>{
      batteryAll.value = res.list;
      batteryParams.value.totalPage = res.totalPage;
      showModal.value = true;
    });
  }

  //分页
  async function batteryChangePage(e){

    //改变一下页数
    batteryParams.value.page = e;
    //重新请求一下接口
    var batteryAll1 = await batteryloglist(batteryParams.value);
    batteryParams.value.totalPage = batteryAll1.totalPage;
    batteryAll.value = batteryAll1.list;
  }

  async function batteryChangePagesize(e){
    //改变条数
    batteryParams.value.pageSize = e;

    var batteryAll1 = await batteryloglist(batteryParams.value);
    batteryParams.value.totalPage = batteryAll1.totalPage;
    batteryAll.value = batteryAll1.list;
  }

  onMounted(async () => {
    
  });
</script>

<style lang="less" scoped></style>
